from pyecharts.charts import Line
import time
import akshare as ak
import pyecharts.options as opts
from pyecharts.globals import ThemeType

# 指数估值 - 历史行情

# idx = "上证50"
# idx = "沪深300"
idx = "恒生指数"
df = ak.index_value_hist_funddb(symbol=idx, indicator="市盈率")
date = time.strftime("%Y%m%d", time.localtime())

title = f"{idx}-市盈率-{date}"

df.to_excel(f"{idx}-市盈率-{date}.xlsx", index=False)

# 折线图
chart = Line(
    # 设置主题、画布宽度、高度等信息
    init_opts=opts.InitOpts(theme=ThemeType.DARK, width="1400px", height="600px"),
)

# 全局设置
chart.set_global_opts(
    title_opts=opts.TitleOpts(title="", ),
    # 工具箱配置
    toolbox_opts=opts.ToolboxOpts(is_show=True),
    # 区域缩放配置
    datazoom_opts=[opts.DataZoomOpts(is_show=True, range_start=0, range_end=100)],
)

y_data = df["市盈率"].tolist()  # y轴数据
chart.add_xaxis(xaxis_data=df['日期'].tolist())   # 添加x轴数据
chart.add_yaxis(
    series_name=title,
    y_axis=y_data,  # y轴数据
    is_connect_nones=True,  # 连接空数据
    markpoint_opts=opts.MarkPointOpts(
        data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")],
    ),
    # 标记线
    markline_opts=opts.MarkLineOpts(
        data=[opts.MarkLineItem(type_="average")]
    ),
)
chart.render(f"{title}.html")
